<!--
 * @Descripttion: (tool/Gen_Config)
 * @Author: (admin)
 * @Date: (2023-10-30)
-->
<template>
  <div>

    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="配置名称" prop="configName">
        <el-select clearable  v-model="queryParams.configName" placeholder="请选择配置名称">
          <el-option v-for="item in  options.tool_config_name " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            <span class="fl">{{ item.dictLabel }}</span>
            <span class="fr" style="color: var(--el-text-color-secondary);">{{ item.dictValue }}</span>
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>


    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['tool:genconfig:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column prop="id" label="主键" align="center" v-if="columns.showColumn('id')"/>

      <el-table-column prop="configName" label="配置名称" align="center" v-if="columns.showColumn('configName')">
        <template #default="scope">
          <dict-tag :options=" options.tool_config_name " :value="scope.row.configName"  />
        </template>
      </el-table-column>

      <el-table-column prop="arrangement" label="排列方式" align="center" v-if="columns.showColumn('arrangement')">
        <template #default="scope">
          <dict-tag :options=" options.udlr_arrangement " :value="scope.row.arrangement"  />
        </template>
      </el-table-column>







      <el-table-column prop="enabled" label="是否启用" align="center" v-if="columns.showColumn('enabled')">
        <template #default="scope">
          <dict-tag :options=" options.sys_notice_status " :value="scope.row.enabled"  />
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('createBy')"/>
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('createTime')"/>
      <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('remark')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button v-hasPermi="['tool:genconfig:edit']" type="success" icon="edit" title="编辑" @click="handleUpdate(scope.row)"></el-button>
          <el-button v-hasPermi="['tool:genconfig:delete']" type="danger" icon="delete" title="删除" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改tool对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item label="配置名称" prop="configName">
              <el-select v-model="form.configName"  placeholder="请选择配置名称">
                <el-option
                  v-for="item in options.tool_config_name"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="排列方式" prop="arrangement">
              <el-select v-model="form.arrangement"  placeholder="请选择排列方式">
                <el-option
                  v-for="item in options.udlr_arrangement"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="parseInt(item.dictValue)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>






          <el-col :lg="12">
            <el-form-item label="Top初始值" prop="initialValueTop">
              <el-input-number v-model.number="form.initialValueTop" :controls="true" controls-position="right" placeholder="请输入Top初始值" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="是否启用" prop="enabled">
              <el-radio-group v-model="form.enabled">
                <el-radio v-for="item in options.sys_notice_status" :key="item.dictValue" :label="parseInt(item.dictValue)">
                  {{item.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>




          <el-col :lg="12">
            <el-form-item label="上下间隔" prop="upDownInterval">
              <el-input-number v-model.number="form.upDownInterval" :controls="true" controls-position="right" placeholder="请输入上下间隔" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="左右间隔" prop="leftRightInterval">
              <el-input-number v-model.number="form.leftRightInterval" :controls="true" controls-position="right" placeholder="请输入左右间隔" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="高度" prop="height">
              <el-input-number v-model.number="form.height" :controls="true" controls-position="right" placeholder="请输入高度" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="输入框宽度" prop="columnWidth">
              <el-input-number v-model.number="form.columnWidth" :controls="true" controls-position="right" placeholder="请输入输入框宽度" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>



          <el-col :lg="12">
            <el-form-item label="字号" prop="fontSize">
              <el-input-number v-model.number="form.fontSize" :controls="true" controls-position="right" placeholder="请输入字号" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="字体名称" prop="fontName">
              <el-input v-model="form.fontName" placeholder="请输入字体名称"  :style="{ width: '180px' }"  />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="一行显示列" prop="numberColumns">
              <el-input-number v-model.number="form.numberColumns" :controls="true" controls-position="right" placeholder="请输入一行显示列" :style="{ width: '180px' }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" :style="{ width: '180px' }"  />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="genconfig">
import { listGenConfig,
  addGenConfig, delGenConfig,
  updateGenConfig,getGenConfig,
}
  from '@/api/tool/genconfig.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  configName: undefined
})
const columns = ref([
  { visible: true, prop: 'id', label: '主键' },
  { visible: true, prop: 'arrangement', label: '排列方式' },
  { visible: true, prop: 'configName', label: '配置名称' },
  { visible: true, prop: 'enabled', label: '是否启用' },
  { visible: true, prop: 'initialValueTop', label: 'Top初始值' },
  { visible: true, prop: 'upDownInterval', label: '上下间隔' },
  { visible: true, prop: 'leftRightInterval', label: '左右间隔' },
  { visible: true, prop: 'height', label: '高度' },
  { visible: true, prop: 'columnWidth', label: '输入框宽度' },
  { visible: false, prop: 'fontName', label: '字体名称' },
  { visible: false, prop: 'fontSize', label: '字号' },
  { visible: false, prop: 'numberColumns', label: '一行显示列' },
  { visible: false, prop: 'createBy', label: '创建人' },
  { visible: false, prop: 'createTime', label: '创建时间' },
  { visible: false, prop: 'updateBy', label: '更新人' },
  { visible: false, prop: 'updateTime', label: '更新时间' },
  { visible: false, prop: 'remark', label: '备注' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
  { dictType: "tool_config_name" },
  { dictType: "sys_notice_status" },
  { dictType: "udlr_arrangement" },
]

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

function getList(){
  loading.value = true
  listGenConfig(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    ConfigName: [{ required: true, message: "配置名称不能为空", trigger: "blur", type: "number" }],
    Arrangement: [{ required: true, message: "排列方式不能为空", trigger: "blur", type: "number" }],
    initialValueTop: [{ required: true, message: "Top初始值不能为空", trigger: "blur", type: "number" }],
    upDownInterval: [{ required: true, message: "上下间隔不能为空", trigger: "blur", type: "number" }],
    leftRightInterval: [{ required: true, message: "左右间隔不能为空", trigger: "blur", type: "number" }],
    height: [{ required: true, message: "高度不能为空", trigger: "blur", type: "number" }],
    columnWidth: [{ required: true, message: "输入框宽度不能为空", trigger: "blur", type: "number" }],
    fontSize: [{ required: true, message: "字号不能为空", trigger: "blur", type: "number" }],
    numberColumns: [{ required: true, message: "一行显示列不能为空", trigger: "blur", type: "number" }],
  },
  options: {
    // 配置名称 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    tool_config_name: [],
    // 是否启用 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    sys_notice_status: [],
    // 排列方式 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    udlr_arrangement: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    configName: null,
    arrangement: null,
    enabled: null,
    initialValueTop: null,
    upDownInterval: null,
    leftRightInterval: null,
    height: null,
    columnWidth: null,
    fontName: null,
    fontSize: null,
    numberColumns: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("formRef")
}
// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加config'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getGenConfig(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改tool'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateGenConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addGenConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
    .then(function () {
      return delGenConfig(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}

handleQuery()
</script>
